package com.ponhu.ea2.stockcenter.vo;

import com.ponhu.ea2.common.basic.util.time.DateUtils;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Wang
 * @date 2022/1/14 3:28 下午
 */
@Data
@Accessors(chain = true)
public class DingTalkContentVO implements Serializable {
    private String title;

    private String path;

    private String traceId;

    private String requestParams;

    private String exceptionType;

    private String exceptionCode;

    private String exceptionMsg;

    private String grafanaUrl;

    private List<DingTalkPropertyVO> propertyVOList = new ArrayList<>();

    public String getDingTalkMarkDownContent() {
        StringBuilder builder = new StringBuilder();

        builder.append("### 库存中心异常").append(exceptionCode).append("，快来人那～\n")
                .append("**路径:** `").append(path).append("`\n\n")
                .append("**时间:** ").append(DateUtils.getCurrentDateTime()).append("\n\n")
                .append("**TraceId:** [").append(traceId).append("](").append(grafanaUrl).append(traceId).append("&from=now-24h&to=now)\n\n")
                .append("**请求参数:**\n")
                .append("> ").append(requestParams).append("\n\n")
                .append("**异常类型:** ").append(exceptionType).append("\n\n")
                .append("**异常代码:** ").append(exceptionCode).append("\n\n")
                .append("**异常信息:**\n\n")
                .append("> ").append(exceptionMsg);

        return builder.toString();
    }

    public DingTalkContentVO addText(String name, String text) {
        return add(name, text, false, null);
    }
    public DingTalkContentVO addText(String name, String text, String linkUrl) {
        return add(name, text, false, linkUrl);
    }

    public DingTalkContentVO addDesc(String name, String text) {
        return add(name, text, true, null);
    }
    public DingTalkContentVO addDesc(String name, String text, String linkUrl) {
        return add(name, text, true, linkUrl);
    }

    private DingTalkContentVO add(String name, String text, boolean isDesc, String linkUrl) {
        DingTalkPropertyVO dingTalkPropertyVO = new DingTalkPropertyVO();
        dingTalkPropertyVO.setName(name).setDesc(isDesc).setText(text).setLinkUrl(linkUrl);

        propertyVOList.add(dingTalkPropertyVO);
        return this;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("### ").append(title).append("\n")
                .append("**路径:** `").append(path).append("`\n\n")
                .append("**时间:** ").append(DateUtils.getCurrentDateTime()).append("\n\n")
                .append("**TraceId:** [").append(traceId).append("](").append(grafanaUrl).append(traceId).append("&from=now-24h&to=now)\n\n");

        if(CollectionUtils.isNotEmpty(propertyVOList)){
            propertyVOList.forEach(propertyVO -> {

                builder.append("**").append(propertyVO.getName()).append(":**");

                String text = propertyVO.getText();
                if(StringUtils.isNotBlank(propertyVO.getLinkUrl())){
                    text = "[" + text + "]" + "(" + propertyVO.getLinkUrl() +")";
                }

                if(propertyVO.isDesc()){
                    builder.append("\n> ").append(text).append("\n\n");
                }else {
                    builder.append(" ").append(text).append("\n\n");
                }
            });
        }

        return builder.toString();
    }

    private List<DingTalkPropertyVO> getPropertyVOList() {
        return propertyVOList;
    }

    private void setPropertyVOList(List<DingTalkPropertyVO> propertyVOList) {
        this.propertyVOList = propertyVOList;
    }
}
